#version 120

subroutine void RenderPassType();
subroutine uniform RenderPassType RenderPass;

attribute vec3  VertexPostion;
attribute vec3  VertexVelocity;
attribute float VertexStartTime;
attribute vec3  VertexInitialVelocity;

out vec3  Position;     // To transform feedback
out vec3  Velocity;     // To transform feedback
out float StartTime;    // To transform feedback
out float Transp;       // To fragment shader


uniform float Time;     // Animation time
uniform float H;        // Elapsed time between frames
uniform vec3 Accel;     // Particle acceleration
uniform vec3 Gravity = vec3(0.0,-0.05,0.0);
uniform float ParticleLifetime; // Max particle lifetime

uniform mat4 MatrixModel;
uniform mat4 MatrixView;
uniform mat4 MatrixProjection;

subroutine (RenderPassType)
void update()
{
  Position = VertexPosition;
  Velocity = VertexVelocity;
  StartTime = VertexStartTime;
  
  if (Time >= StartTime) {
    float age = Time - StartTime;
    if (age > ParticleLifetime) {
      // The particle is past its lifetime, recycle
      Position = vec3(0.0);
      Velocity = VertexInitialVelocity;
      StartTime = Time;
    } else {
      // The particle is alive, update
      Position += Velocity * H;
      Velocity += Accel * H;
    }
  }
}

subroutine (RenderPassType)
void render()
{
  float age = Time - VertexStartTime;
  Transp = 1.0 - age / ParticleLifetime;

  gl_Position = MatrixProjection * MatrixView * MatrixModel *
                vec4(VertexPosition, 1.0);
}

void main()
{
  // This will call either render() or update()
  RenderPass();
}